{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "# Python 文件I/O"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入：读取键盘输入\n",
      "你输入的内容是： 读取键盘输入\n"
     ]
    }
   ],
   "source": [
    "str = input('请输入：')\n",
    "\n",
    "print('你输入的内容是：',str)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### open 函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\n",
    "open() 方法\n",
    "Python open() 方法用于打开一个文件，并返回文件对象，在对文件进行处理过程都需要使用到这个函数，如果该文件无法被打开，会抛出 OSError。\n",
    "\n",
    "注意：使用 open() 方法一定要保证关闭文件对象，即调用 close() 方法。\n",
    "\n",
    "open() 函数常用形式是接收两个参数：文件名(file)和模式(mode)。\n",
    "\n",
    "\n",
    "open(file, mode='r')\n",
    "\n",
    "\n",
    "完整的语法格式为：\n",
    "\n",
    "open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\n",
    "参数说明:\n",
    "\n",
    "file: 必需，文件路径（相对或者绝对路径）。\n",
    "mode: 可选，文件打开模式\n",
    "buffering: 设置缓冲\n",
    "encoding: 一般使用utf8\n",
    "errors: 报错级别\n",
    "newline: 区分换行符\n",
    "closefd: 传入的file参数类型\n",
    "opener:\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "读取test2的内容是：2019 - 07 - 22  python 璇诲彇鏂囦欢\n"
     ]
    }
   ],
   "source": [
    "# 必须先用Python内置的open()函数打开一个文件，创建一个file对象，相关的方法才可以调用它进行读写。\n",
    "\n",
    "'''\n",
    "\n",
    "fo = open('test_new1','w',buffering=-1,encoding='UTF-8')\n",
    "print('文件是：', fo.name)\n",
    "print('文件是否关闭：', fo.closed)\n",
    "fo.close()\n",
    "print('文件是否关闭：', fo.closed)\n",
    "fo = open('test_new1','w',buffering=-1,encoding='UTF-8')# 注意：如果没有指定 encoding 将无法写入内容\n",
    "fo.write('新添加的内容')\n",
    "fo.close()\n",
    "\n",
    "'''\n",
    "\n",
    "fo = open('test2','r+')\n",
    "text1 = fo.read()\n",
    "print('读取test2的内容是：2019 - 07 - 22 ',text1)\n",
    "\n",
    "# file_position = fo.tell()\n",
    "\n",
    "# print('文件位置：',file_position)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 重命名和删除文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "'''\n",
    "\n",
    "python的os模块提供了帮你执行文件处理操作的方法，比如重命名和删除文件。\n",
    "\n",
    "要使用这个模块，你必须先导入它，然后才可以调用相关的各种功能。\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "\n",
    "# 重命名文件test1到test_new1\n",
    "# os.rename('test1','test_new1')\n",
    "cwd = os.getcwd()\n",
    "print('显示当前的工作目录 getcwd :  ',cwd)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
